﻿//--------------------------------------------------------------------------------
// 文件描述：数据转换类
// 文件作者：
// 创建日期：
//--------------------------------------------------------------------------------
namespace DataCollectService
{
    using System;
    using System.Security.Cryptography;
    using System.Text;

    /// <summary>
    /// 数据转换类 
    /// </summary>
    public static class DataConverter
    {
        /// <summary>
        /// 将输入的字符串转换为布尔类型
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <returns>返回布尔值</returns>
        public static bool ToBoolean(this string input)
        {
            bool output = false;
            try
            {
                if (!string.IsNullOrEmpty(input))
                {
                    input = input.Trim();
                    if (string.Compare(input, "true", StringComparison.OrdinalIgnoreCase) == 0
                        || string.Compare(input, "yes", StringComparison.OrdinalIgnoreCase) == 0
                        || string.Compare(input, "1", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        output = true;
                    }
                }
            }
            catch { }

            return output;
        }

        /// <summary>
        /// 将输入的字符串转换为布尔类型
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <returns>返回布尔值</returns>
        public static bool ToBoolean(this object input)
        {
            bool output = false;
            try
            {
                string ninput = input.ToString();
                if (!string.IsNullOrEmpty(ninput))
                {
                    input = ninput.Trim();
                    if (string.Compare(ninput, "true", StringComparison.OrdinalIgnoreCase) == 0
                        || string.Compare(ninput, "yes", StringComparison.OrdinalIgnoreCase) == 0
                        || string.Compare(ninput, "1", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        output = true;
                    }
                }
            }
            catch { }

            return output;
        }

        /// <summary>
        /// 将输入的对象转换为日期时间类型
        /// </summary>
        /// <param name="input">输入的对象</param>
        /// <returns>转换成功则返回对应日期时间，否则返回当前日期时间</returns>
        public static DateTime ToDate(this object input)
        {
            return (Convert.IsDBNull(input) || object.Equals(input, null)) ? DateTime.Now : input.ToString().ToDate();
        }

        /// <summary>
        /// 将输入的字符串转换为日期时间类型
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <returns>转换成功并且日期大于数据库datetime类型的最小时间（1753年1月1日）则返回对应日期时间，否则返回当前日期时间</returns>
        public static DateTime ToDate(this string input)
        {
            DateTime output;
            if (!DateTime.TryParse(input, out output) || output < new DateTime(1753, 1, 1, 12, 0, 0))
            {
                output = DateTime.Now;
            }

            return output;
        }

        /// <summary>
        /// 将输入的字符串转换为可空的日期时间类型
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <param name="outTime">指定的日期时间</param>
        /// <returns>转换成功则返回对应的日期时间，否则返回指定的日期时间</returns>
        public static DateTime? ToDate(this string input, DateTime? outTime)
        {
            DateTime output;
            if (!DateTime.TryParse(input, out output))
            {
                return outTime;
            }
            else
            {
                return output;
            }
        }

        /// <summary>
        /// 将输入的字符串类型化为日期时间字符串
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <returns>转换成功则返回对应日期时间字符串，否则返回空字符串</returns>
        public static string ToDateString(this string input)
        {
            DateTime output;
            if (!DateTime.TryParse(input, out output))
            {
                return string.Empty;
            }
            else
            {
                return output.ToString("yyyy-MM-dd");
            }
        }

        /// <summary>
        /// 将输入的对象转换为Decimal类型
        /// </summary>
        /// <param name="input">输入的对象</param>
        /// <returns>转换成功则返回对应十进制数，否则返回 0</returns>
        public static decimal ToDecimal(this object input)
        {
            return input.ToDecimal(0);
        }

        /// <summary>
        /// 将输入的对象转换为Decimal类型
        /// </summary>
        /// <param name="input">输入的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换成功则返回对应十进制数，否则返回默认值</returns>
        public static decimal ToDecimal(this object input, decimal defaultValue)
        {
            return (Convert.IsDBNull(input) || object.Equals(input, null)) ? 0 : input.ToString().ToDecimal(defaultValue);
        }

        /// <summary>
        /// 将输入的字符串转换为Decimal类型
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <returns>转换成功则返回对应十进制数，否则返回 0</returns>
        public static decimal ToDecimal(this string input)
        {
            return input.ToDecimal(0);
        }

        /// <summary>
        /// 将输入的字符串转换为Decimal类型
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换成功则返回对应十进制数，否则返回默认值</returns>
        public static decimal ToDecimal(this string input, decimal defaultValue)
        {
            decimal output;
            if (!Decimal.TryParse(input, out output))
            {
                output = defaultValue;
            }

            return output;
        }

        /// <summary>
        /// 将输入的对象转换为Double类型
        /// </summary>
        /// <param name="input">输入的对象</param>
        /// <returns>转换成功则返回对应浮点数字，否则返回 0</returns>
        public static double ToDouble(this object input)
        {
            return input.ToDouble(0);
        }

        /// <summary>
        /// 将输入的对象转换为Double类型
        /// </summary>
        /// <param name="input">输入的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换成功则返回对应浮点数字，否则返回默认值</returns>
        public static double ToDouble(this object input, double defaultValue)
        {
            return (Convert.IsDBNull(input) || object.Equals(input, null)) ? 0 : input.ToString().ToDouble(defaultValue);
        }

        /// <summary>
        /// 将输入的字符串转换为Double类型
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <returns>转换成功则返回对应浮点数字，否则返回 0</returns>
        public static double ToDouble(this string input)
        {
            return input.ToDouble(0);
        }

        /// <summary>
        /// 将输入的字符串转换为Double类型
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换成功则返回对应浮点数字，否则返回默认值</returns>
        public static double ToDouble(this string input, double defaultValue)
        {
            double output;
            if (!Double.TryParse(input, out output))
            {
                return defaultValue;
            }

            return output;
        }

        /// <summary>
        /// 将输入的对象转换为Int32类型
        /// </summary>
        /// <param name="input">输入的对象</param>
        /// <returns>转换成功则返回对应数字，否则返回 0</returns>
        public static int ToInt32(this object input)
        {
            return input.ToInt32(0);
        }

        /// <summary>
        /// 将输入的对象转换为Int32类型
        /// </summary>
        /// <param name="input">输入的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换成功则返回对应数字，否则返回默认值</returns>
        public static int ToInt32(this object input, int defaultValue)
        {
            return (Convert.IsDBNull(input) || object.Equals(input, null)) ? defaultValue : input.ToString().ToInt32(defaultValue);
        }

        /// <summary>
        /// 将输入的字符串转换为Int32类型
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <returns>转换成功则返回对应数字，否则返回 0</returns>
        public static int ToInt32(this string input)
        {
            return input.ToInt32(0);
        }

        /// <summary>
        /// 将输入的字符串转换为Int32类型
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换成功则返回对应数字，否则返回默认值</returns>
        public static int ToInt32(this string input, int defaultValue)
        {
            int output;
            if (!Int32.TryParse(input, out output))
            {
                output = defaultValue;
            }

            return output;
        }

        /// <summary>
        /// 转换为long类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static long ToLong(this object input)
        {
           
            return ToLong(input, 0);
        }

        /// <summary>
        /// 转换为long
        /// </summary>
        /// <param name="input"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static long ToLong(object input, long defaultValue)
        {
            return (Convert.IsDBNull(input) || object.Equals(input, null)) ? defaultValue : ToLong(input.ToString(), defaultValue);
        }

        /// <summary>
        /// 转换为long
        /// </summary>
        /// <param name="input"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static long ToLong(string input, long defaultValue)
        {
            long output;
            if (!long.TryParse(input, out output))
            {
                output = defaultValue;
            }
            return output;
        }
        /// <summary>
        /// 将输入的对象转换为float类型
        /// </summary>
        /// <param name="input">输入的对象</param>
        /// <returns>转换成功则返回对应单精度浮点数字，否则返回 0</returns>
        public static float ToSingle(this object input)
        {
            return input.ToSingle(0);
        }

        /// <summary>
        /// 将输入的对象转换为float类型
        /// </summary>
        /// <param name="input">输入的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换成功则返回对应单精度浮点数字，否则返回默认值</returns>
        public static float ToSingle(this object input, float defaultValue)
        {
            return (Convert.IsDBNull(input) || object.Equals(input, null)) ? 0 : input.ToString().ToSingle(defaultValue);
        }

        /// <summary>
        /// 将输入的字符串转换为float类型
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <returns>转换成功则返回对应单精度浮点数字，否则返回 0</returns>
        public static float ToSingle(this string input)
        {
            //
            return input.ToSingle(0);
        }
        
        /// <summary>
        /// 将输入的字符串转换为float类型
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>转换成功则返回对应单精度浮点数字，否则返回默认值</returns>
        public static float ToSingle(this string input, int defaultValue)
        {
            long output;
            if (!Int64.TryParse(input, out output))
            {
                output = defaultValue;
            }

            return output;
        }

        /// <summary>
        /// 获取MD5加密字符串
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public static string GetMD5String(string strKey)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strKey, "MD5");
        }

        /// <summary>
        /// 获取MD5加密字符串
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public static string GetSHA1String(string strKey)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strKey, "SHA1");
        }

        public static string MD5PWD(String PWD)
        {
            using (MD5 mi = MD5.Create())
            {
                byte[] buffer = Encoding.Default.GetBytes(PWD);
                byte[] newbuffer = mi.ComputeHash(buffer);
                StringBuilder SB = new StringBuilder();
                for (int i = 0; i < newbuffer.Length; i++)
                {
                    SB.Append(newbuffer[i].ToString("x2"));
                }
                return SB.ToString();
            }
        }
    }
}
